package net.lz1998.pbbot.plugin;

import net.lz1998.pbbot.bot.Bot;
import net.lz1998.pbbot.bot.BotPlugin;
import net.lz1998.pbbot.utils.Msg;
import onebot.OnebotEvent;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;

@Component
public class HelloPlugin extends BotPlugin {

    @Override
    public int onPrivateMessage(@NotNull Bot bot, @NotNull OnebotEvent.PrivateMessageEvent event) {
        // 这里展示了event消息链的用法
       /* List<OnebotBase.Message> messageChain = event.getMessageList();
        if (messageChain.size() > 0) {
            OnebotBase.Message message = messageChain.get(0);
            if (message.getType().equals("text")) {
                String text = message.getDataMap().get("text");
                if ("hello".equals(text)) {
                    bot.sendPrivateMsg(event.getUserId(), "hi", false);
                }
            }
        }*/
        return MESSAGE_IGNORE;
    }

    @Override
    public int onGroupMessage(@NotNull Bot bot, @NotNull OnebotEvent.GroupMessageEvent event) {
        String message = event.getRawMessage().trim();
        Long groupId = event.getGroupId();
//        if (message.startsWith("文字转语音")) {
//            String msg = message.replace("文字转语音", "").trim();
//            String domain = "https://api.oick.cn/txt/apiz.php?text=%s&spd=3";
//            try {
//                msg = URLEncoder.encode(msg, "utf-8");
//                String u = String.format(domain, msg);
//                URL url = new URL(u);
//                URLConnection connection = url.openConnection();
//                InputStream inputStream = connection.getInputStream();
//                File file = new File("E:/phpstudy_pro/WWW/mp3/");
//                if (!file.exists()) {
//                    file.mkdirs();
//                }
//                String fileName = String.valueOf(new Date().getTime());
//                String filePath = "E:/phpstudy_pro/WWW/mp3/" + fileName + ".mp3";
//                FileOutputStream fs = new FileOutputStream(filePath);
//                byte[] buffer = new byte[1204];
//                int byteread = 0;
//                while ((byteread = inputStream.read(buffer)) != -1) {
//                    fs.write(buffer, 0, byteread);
//                }
//                File source = new File("E:/phpstudy_pro/WWW/mp3/QQ约会高中女生.mp3");
//                //File target = new File("E:/phpstudy_pro/WWW/mp3/" + fileName + ".amr");
//                File target = new File("E:/phpstudy_pro/WWW/mp3/QQ约会高中女生.amr");
//                AudioAttributes audio = new AudioAttributes();
//                audio.setCodec("libamr_nb");
//                audio.setBitRate(12200);//比特率
//                audio.setChannels(1);
//                audio.setSamplingRate(8000);
//                EncodingAttributes attrs = new EncodingAttributes();
//                attrs.setFormat("amr");
//                attrs.setAudioAttributes(audio);
//                Encoder encoder = new Encoder();
//                encoder.encode(source, target, attrs);
//                Msg.builder()
//                        .record("http://47.93.130.192:8088/mp3/QQ约会高中女生.amr")
//                        .sendToGroup(bot, groupId);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
        return MESSAGE_IGNORE;
    }

    @Async(value = "lazyGroupExecutor")
    public void messageGroupPush(String s, Bot bot, long groupId, long id) throws InterruptedException {
        System.out.println("当前线程:"+Thread.currentThread().getName());
        if (s.length() > 15) {
            TimeUnit.SECONDS.sleep(8);
        } else if (s.length() > 10) {
            TimeUnit.SECONDS.sleep(6);
        } else {
            TimeUnit.SECONDS.sleep(4);
        }
        if (StringUtils.isBlank(s)) {
            bot.sendGroupMsg(groupId, Msg.builder().at(id).text("不知道回点啥..."), false);
        } else {
            bot.sendGroupMsg(groupId, Msg.builder().at(id).text(s), false);
        }
    }

    @Async(value = "lazyPrivateExecutor")
    public void  messagePrivatePush(String info, Bot bot, OnebotEvent.PrivateMessageEvent event) throws InterruptedException {
        System.out.println("当前线程:"+Thread.currentThread().getName());
        if (info.length() > 15) {
            TimeUnit.SECONDS.sleep(8);
        } else if (info.length() > 10) {
            TimeUnit.SECONDS.sleep(6);
        } else {
            TimeUnit.SECONDS.sleep(4);
        }
        if (StringUtils.isBlank(info)) {
            bot.sendPrivateMsg(event.getUserId(), Msg.builder().text("不知道回点啥..."), false);
        } else {
            bot.sendPrivateMsg(event.getUserId(), Msg.builder().text(info), false);
        }
    }

    @Async(value = "lazyPrivateFaceExecutor")
    public void messagePrivateFacePush(Bot bot, OnebotEvent.PrivateMessageEvent event, Msg face) throws InterruptedException {
        System.out.println("当前线程:"+Thread.currentThread().getName());
        TimeUnit.SECONDS.sleep(4);
        bot.sendPrivateMsg(event.getUserId(), face, false);
    }

    @Async(value = "lazyPrivateImageExecutor")
    public void messageImageFacePush(Bot bot, OnebotEvent.PrivateMessageEvent event, Msg image) throws InterruptedException {
        System.out.println("当前线程:"+Thread.currentThread().getName());
        TimeUnit.SECONDS.sleep(3);
        bot.sendPrivateMsg(event.getUserId(), image, false);
    }

    @Async(value = "lazyGroupFaceExecutor")
    public void messageGroupFacePush(long groupId, Bot bot, long id, String number) throws InterruptedException {
        System.out.println("当前线程:"+Thread.currentThread().getName());
        TimeUnit.SECONDS.sleep(4);
        bot.sendGroupMsg(groupId, Msg.builder().at(id).face(Integer.parseInt(number)), false);
    }

    @Async(value = "lazyGroupImageExecutor")
    public void messageGroupImagePush(Bot bot, long groupId, Msg image) throws InterruptedException {
        System.out.println("当前线程:"+Thread.currentThread().getName());
        TimeUnit.SECONDS.sleep(3);
        bot.sendGroupMsg(groupId, image, false);
    }
}
